package com.sjgtw.cloud.web.reservoir.controller;

import java.util.ArrayList;
import java.util.List;

import com.sjgtw.cloud.common.core.utils.StringUtils;
import com.sjgtw.cloud.common.core.web.controller.BaseController;
import com.sjgtw.cloud.common.core.web.domain.AjaxResult;
import com.sjgtw.cloud.common.core.web.page.TableDataInfo;
import com.sjgtw.cloud.util.EasyExcelUtil;
import com.sjgtw.cloud.web.reservoir.domain.Reservoir;
import com.sjgtw.cloud.web.reservoir.service.IReservoirService;
import com.sjgtw.cloud.web.waterDamage.domain.WaterDamage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;

/**
 * 水库分布Controller
 * 
 * @author ruoyi
 * @date 2024-03-11
 */
@RestController
@Api(tags = "1.9、水库分布管理")
@RequestMapping("/reservoir")
public class ReservoirController extends BaseController
{
    @Autowired
    private IReservoirService reservoirService;

    /**
     * 查询水库分布列表
     */
   // @RequiresPermissions("system:reservoir:list")
    @GetMapping("/list")
    @ApiModelProperty("列表")
    public TableDataInfo list(Reservoir reservoir)
    {
        startPage();
        List<Reservoir> list = reservoirService.selectReservoirList(reservoir);
        return getDataTable(list);
    }

    /**
     * 导出水库分布列表
     */
/*    @RequiresPermissions("system:reservoir:export")
    @Log(title = "水库分布", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Reservoir reservoir)
    {
        List<Reservoir> list = reservoirService.selectReservoirList(reservoir);
        ExcelUtil<Reservoir> util = new ExcelUtil<Reservoir>(Reservoir.class);
        util.exportExcel(response, list, "水库分布数据");
    }*/

    /**
     * 获取水库分布详细信息
     */
    //@RequiresPermissions("system:reservoir:query")
    @GetMapping(value = "/getInfo/{id}")
    @ApiModelProperty("获取水库分布详细信息")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(reservoirService.selectReservoirById(id));
    }

    /**
     * 新增水库分布
     */
    //@RequiresPermissions("system:reservoir:add")
    //@Log(title = "水库分布", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ApiModelProperty("新增")
    public AjaxResult add(@RequestBody Reservoir reservoir)
    {
        return toAjax(reservoirService.insertReservoir(reservoir));
    }

    /**
     * 修改水库分布
     */
    //@RequiresPermissions("system:reservoir:edit")
    //@Log(title = "水库分布", businessType = BusinessType.UPDATE)
    @PutMapping("/edit")
    @ApiModelProperty("修改")
    public AjaxResult edit(@RequestBody Reservoir reservoir)
    {
        return toAjax(reservoirService.updateReservoir(reservoir));
    }

    /**
     * 删除水库分布
     */
    //@RequiresPermissions("system:reservoir:remove")
    //@Log(title = "水库分布", businessType = BusinessType.DELETE)
	@DeleteMapping("/delete/{id}")
    @ApiModelProperty("删除")
    public AjaxResult remove(@PathVariable Long id)
    {
        return toAjax(reservoirService.deleteReservoirById(id));
    }

    /**
     * 导出
     */
    @ApiOperation(value = "导出")
    @PostMapping("/export")
    public void export(HttpServletResponse response, Reservoir reservoir)
    {
        List<Reservoir> list = reservoirService.selectReservoirList(reservoir);
        EasyExcelUtil<Reservoir> util = new EasyExcelUtil<>(Reservoir.class);
        util.exportExcel(response, list, "水库分布", "水库分布", "reservoir.xlsx");
    }

    /**
     * 下载模版
     */
    @ApiOperation(value = "下载模版")
    @PostMapping("/downloadTemplate")
    public void downloadTemplate(HttpServletResponse response)
    {
        EasyExcelUtil<WaterDamage> util = new EasyExcelUtil<>(WaterDamage.class);
        util.exportExcel(response, new ArrayList<>(), "水库分布", "水库分布模版", "reservoir.xlsx");
    }

    /**
     * 导入
     */
    @ApiOperation(value = "导入")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file)
    {
        EasyExcelUtil<Reservoir> util = new EasyExcelUtil<>(Reservoir.class);
        List<Reservoir> entities = util.readExcel(file, 0, 1);
        if (entities == null || entities.size() == 0) {
            return error("没有读取到数据");
        }

        List<String> errors = reservoirService.check(entities);
        if (errors.size() > 0) {
            return AjaxResult.error("", errors);
        }

        reservoirService.batchSave(entities);
        return success();
    }


}
